package com.mrxu.monitor.util;

import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Component
public class RequestUtil {

    /**
     * 获取请求ip地址
     * @param request
     * @return ip地址
     */
    public String getClientIP(HttpServletRequest request) {
        return ServletUtil.getClientIP(request);
    }

    /**
     * 获取操作系统
     * @param request
     * @return
     */
    public String getOs(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if(userAgent.contains("Windows")) {
            return "Windows";
        }
        if(userAgent.contains("Macintosh")) {
            return "Mac";
        }
        if(userAgent.contains("Android")) {
            return "Android";
        }
        if(userAgent.contains("iPhone")) {
            return "iPhone";
        }
        if(userAgent.startsWith("curl")) {
            return "linux";
        }
        log.warn("未获取到操作系统:{}",userAgent);
        return "其他";
    }

    /**
     * 获取浏览器名字
     * @param request
     * @return
     */
    public String getBrowser(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if(userAgent.startsWith("curl")) {
            return "curl";
        }
        if(userAgent.contains("QQBrowser")) {
            return "QQ";
        }
        if(userAgent.contains("MicroMessenger")) {
            return "微信";
        }
        if(userAgent.contains("Edge")) {
            return "Edge";
        }
        if(userAgent.contains("360SE") || userAgent.contains("360EE")) {
            return "360";
        }
        if(userAgent.contains("MSIE")) {
            return "IE";
        }
        if(userAgent.contains("Firefox")) {
            return "Firefox";
        }
        if(userAgent.contains("Chrome")) {
            return "Chrome";
        }
        if(userAgent.contains("Safari")) {
            return "Safari";
        }
        log.warn("未获取到浏览器类型:{}",userAgent);
        return "未知";
    }



    public boolean isMobile(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.contains("Mobile")) {
            return true;
        }
        return false;
    }


}
